import { page, insertOrUpdate, del, logs, imgs } from '../services/fixedAssets';

import { selectList } from '../services/tmsUser';

import React from 'react';
import { Input, message, Select } from 'antd';

const { Option } = Select;

/**
 * 客户数据模型
 */

//表格列表信息
const columns = [
  {
    title: '资产名称',
    dataIndex: 'name',
    width: '120px',
    editable: true,
    ellipsis: true,
  },
  {
    title: '资产详情',
    dataIndex: 'info',
    width: '180px',
    editable: true,
    ellipsis: true,
  },
  {
    title: '资产分类',
    dataIndex: 'type',
    width: '140px',
    editable: true,
    ellipsis: true,
    render: text => {
      switch (text) {
        case 0:
          return '固定资产';
        case 1:
          return '耗材资产';
        default:
          return '';
      }
    },
  },
  {
    title: '数量',
    dataIndex: 'amount',
    width: '120px',
    editable: true,
    ellipsis: true,
  },
  {
    title: '单位',
    dataIndex: 'unit',
    width: '120px',
    editable: true,
    ellipsis: true,
  },
  {
    title: '购买时间',
    dataIndex: 'buyTime',
    width: '210px',
    editable: true,
    ellipsis: true,
  },
  {
    title: '购买价格',
    dataIndex: 'buyPrice',
    width: '140px',
    editable: true,
    ellipsis: true,
  },
  {
    title: '所属部门',
    dataIndex: 'deptNo',
    width: '140px',
    editable: true,
    ellipsis: true,
  },
  {
    title: '确认人',
    dataIndex: 'comfirmUser',
    width: '140px',
  },
  {
    title: '确认时间',
    dataIndex: 'comfirmTime',
    width: '200px',
  },
  {
    title: '当前使用人姓名',
    dataIndex: 'usedName',
    width: '140px',
  },
];

const columnsLog = [
  {
    title: '变动人员',
    dataIndex: 'changeUser',
    width: '150px',
  },
  {
    title: '人员姓名',
    dataIndex: 'changeUserName',
    width: '150px',
  },
  {
    title: '变动时间',
    dataIndex: 'changeTime',
    width: '120px',
  },
  {
    title: '变动数量',
    dataIndex: 'changeAmount',
    width: '120px',
  },
  {
    title: '剩余数量',
    dataIndex: 'surplusAmount',
    width: '120px',
  },
  {
    title: '备注',
    dataIndex: 'remark',
    width: '180px',
  },
];

// 搜索框字段
const searchFields = [
  {
    cols: [
      {
        md: 4,
        label: '资产名称',
        name: 'name',
        component: <Input placeholder="请输入资产名称" />,
      },
      {
        md: 4,
        label: '资产类型',
        name: 'type',
        component: (
          <Select placeholder="请选择资产类型">
            <Option value={undefined}>请选择资产类型</Option>
            <Option value={0}>固定资产</Option>
            <Option value={1}>耗材资产</Option>
          </Select>
        ),
      },
    ],
  },
];

// 校验规则
const requiredCols = {
  车牌号: [
    {
      required: 'true',
      message: `请输入车牌号!`,
    },
  ],
};

export default {
  namespace: 'fixedAssets',

  state: {
    searchFields: searchFields, // 搜索框信息
    searchValues: {}, // 搜索框里的值
    pageList: [], // 表格数据
    pagination: {
      pageSize: 10,
      current: 1,
      size: 'small',
      showSizeChanger: true,
      showQuickJumper: true,
    }, // 表格分布数据
    columns: columns, // 表格列信息
    columnsLog: columnsLog,
    editId: '', // 编辑行id
    selectionIds: [], // 表格选择ID
    selectedRows: [], // 表格选择行
    requiredCols: requiredCols, // 必填字段
    tmsUserList: [], // 用户列表
  },

  effects: {
    // 获取分页数据
    *page({ payload }, { call, put }) {
      const res = yield call(page, payload);
      yield put({
        type: 'page_r',
        payload: res,
      });
    },
    *save({ payload }, { call, put }) {
      const res = yield call(insertOrUpdate, payload);
      yield put({
        type: 'save_r',
        payload: res,
      });
    },
    *del({ payload }, { call, put }) {
      const res = yield call(del, payload);
      yield put({
        type: 'del_r',
        payload: res,
      });
    },
    *selectUserList({}, { call, put }) {
      const res = yield call(selectList);
      yield put({
        type: 'userList_r',
        payload: res,
      });
    },
    *logs({ payload }, { call, put }) {
      const res = yield call(logs, payload);
      yield put({
        type: 'save_r',
        payload: res,
      });
    },
    *imgs({ payload }, { call, put }) {
      const res = yield call(imgs, payload);
      yield put({
        type: 'save_r',
        payload: res,
      });
    }
  },

  reducers: {
    // 新增
    handleAdd(state) {
      const { pageList, pagination } = state;
      let addList = pageList.filter(page => {
        return page.id === 0;
      });
      if (addList.length) {
        message.warning('请提交后再添加！');
        return {
          ...state,
        };
      } else {
        pageList.unshift({ id: 0, customerName: '新增' });
        const newPagination = {
          pages: pagination.pages,
          pageSize: pagination.pageSize,
          total: pagination.total + 1,
          current: pagination.current,
          size: 'small',
          showSizeChanger: true,
          showQuickJumper: true,
        };
        return {
          ...state,
          pageList: pageList,
          pagination: newPagination,
        };
      }
    },
    // 绑定搜索框数据
    handleSearchChange(state, action) {
      const { searchValues } = state;
      const newSearchForm = {
        ...searchValues,
        ...action.payload,
      };
      return {
        ...state,
        searchValues: newSearchForm,
      };
    },
    // 处理编辑
    handleEdit(state, action) {
      return {
        ...state,
        editId: action.payload,
      };
    },

    // 改变分页插件
    handlePagination(state, action) {
      const { payload } = action;
      const { pagination } = state;
      let newPagination = {
        ...pagination,
        ...payload,
      };
      return {
        ...state,
        pagination: newPagination,
      };
    },
    // 表格选中Id
    handleSelection(state, action) {
      const { payload } = action;
      return {
        ...state,
        selectionIds: payload.selectedRowKeys,
        selectedRows: payload.selectedRows,
      };
    },
    // 分页数据请求回来后处理
    page_r(state, action) {
      const { payload } = action;
      if (payload && payload.code === 0) {
        const list = payload.result.records;
        const pagination = {
          pages: payload.result.pages,
          pageSize: payload.result.size,
          total: payload.result.total,
          current: payload.result.current,
          size: 'small',
          showSizeChanger: true,
          showQuickJumper: true,
        };
        return {
          ...state,
          pagination: pagination,
          pageList: list,
          editId: '',
          selectionIds: [],
          selectedRows: [],
        };
      } else {
        return {
          ...state,
        };
      }
    },
    // 处理保存
    save_r(state, action) {
      const { payload } = action;
      if (payload && payload.code === 0) {
        message.success('保存成功！');
      } else {
        message.error(`${payload.errorMsg}` ? `${payload.errorMsg}` : '保存失败！');
      }
      return {
        ...state,
      };
    },
    // 处理删除
    del_r(state, action) {
      const { payload } = action;
      if (payload && payload.code === 0) {
        message.success('删除成功！');
      } else {
        message.error(`${payload.errorMsg}` ? `${payload.errorMsg}` : '删除失败！');
      }
      return {
        ...state,
      };
    },

    userList_r(state, action) {
      const { payload } = action;
      if (payload && payload.code === 0) {
        return {
          ...state,
          tmsUserList: payload.result,
        };
      } else {
        message.error(`${payload.errorMsg}` ? `${payload.errorMsg}` : '获取用户列表失败！');
        return {
          ...state,
        };
      }
    },
  },
};
